package org.apache.flink.runtime.metrics.groups;

import static org.apache.flink.util.Preconditions.checkNotNull;

import java.util.Collections;

import javax.annotation.Nullable;

import org.apache.flink.annotation.Internal;
import org.apache.flink.api.common.ConsumerID;
import org.apache.flink.metrics.CharacterFilter;
import org.apache.flink.runtime.metrics.MetricRegistry;
import org.apache.flink.runtime.metrics.dump.QueryScopeInfo;

/**
 * Special {@link org.apache.flink.metrics.MetricGroup} representing everything belonging to a
 * specific consumer, running on the ConsumerManager.
 */
@Internal
public class ConsumerManagerConsumerMetricGroup extends ComponentMetricGroup<ConsumerManagerMetricGroup> {
	
	private final ConsumerID consumerId;
	
    ConsumerManagerConsumerMetricGroup(
            MetricRegistry registry,
            ConsumerManagerMetricGroup parent,
            ConsumerID consumerId,
            @Nullable String consumerName) {
        super(
                registry,
                registry.getScopeFormats()
                        .getConsumerManagerConsumerFormat()
                        .formatScope(checkNotNull(parent), consumerId, consumerName),
                parent);
        this.consumerId = consumerId;
    }

    public final ConsumerManagerMetricGroup parent() {
        return parent;
    }

    // ------------------------------------------------------------------------
    //  Component Metric Group Specifics
    // ------------------------------------------------------------------------

    @Override
    protected Iterable<? extends ComponentMetricGroup> subComponents() {
        return Collections.emptyList();
    }

	@Override
	protected String getGroupName(CharacterFilter filter) {
		return "consumer";
	}

	@Override
	protected QueryScopeInfo createQueryServiceMetricInfo(CharacterFilter filter) {
        return new QueryScopeInfo.ConsumerQueryScopeInfo(this.consumerId.toString());
	}
}
